#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Generator, Dict, Any

from fastapi import Depends

from app.core.logging import get_logger
from app.repositories.mysql import get_db_session
from app.repositories.milvus import get_milvus_client
from app.services.embedding import get_embedding_manager
from app.services.llm import get_llm_manager

# 创建日志记录器
logger = get_logger("dependencies")


async def get_repositories(
    db_session=Depends(get_db_session),
    milvus_client=Depends(get_milvus_client)
) -> Dict[str, Any]:
    """获取所有仓库依赖
    
    Args:
        db_session: 数据库会话
        milvus_client: Milvus客户端
        
    Returns:
        Dict[str, Any]: 包含所有仓库的字典
    """
    # 导入在这里以避免循环导入
    from app.repositories.project import ProjectRepository
    from app.repositories.training_data import TrainingDataRepository
    from app.repositories.vector import VectorRepository
    from app.repositories.query_history import QueryHistoryRepository
    
    # 创建仓库实例
    project_repo = ProjectRepository(db_session)
    training_data_repo = TrainingDataRepository(db_session)
    vector_repo = VectorRepository(milvus_client)
    query_history_repo = QueryHistoryRepository(db_session)
    
    return {
        "project_repo": project_repo,
        "training_data_repo": training_data_repo,
        "vector_repo": vector_repo,
        "query_history_repo": query_history_repo
    }


async def get_services(
    repositories=Depends(get_repositories),
    embedding_manager=Depends(get_embedding_manager),
    llm_manager=Depends(get_llm_manager)
) -> Dict[str, Any]:
    """获取所有服务依赖
    
    Args:
        repositories: 仓库依赖
        embedding_manager: 嵌入管理器
        llm_manager: LLM管理器
        
    Returns:
        Dict[str, Any]: 包含所有服务的字典
    """
    # 导入在这里以避免循环导入
    from app.services.project import ProjectService
    from app.services.training_data import TrainingDataService
    from app.services.text2sql import Text2SQLService
    from app.services.correction import SQLCorrectionService
    from app.services.query_history import QueryHistoryService
    
    # 创建服务实例
    project_service = ProjectService(repositories["project_repo"])
    training_data_service = TrainingDataService(
        repositories["training_data_repo"],
        repositories["vector_repo"],
        embedding_manager
    )
    text2sql_service = Text2SQLService(
        repositories["training_data_repo"],
        repositories["vector_repo"],
        repositories["query_history_repo"],
        embedding_manager,
        llm_manager
    )
    correction_service = SQLCorrectionService(
        repositories["training_data_repo"],
        repositories["vector_repo"],
        repositories["query_history_repo"],
        embedding_manager,
        llm_manager
    )
    query_history_service = QueryHistoryService(repositories["query_history_repo"])
    
    return {
        "project_service": project_service,
        "training_data_service": training_data_service,
        "text2sql_service": text2sql_service,
        "correction_service": correction_service,
        "query_history_service": query_history_service
    }